//
//  DBUserAccount.swift
//  大波微博swift
//
//  Created by 大波 on 16/2/28.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

class DBUserAccount: NSObject, NSCoding {
    
    //用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    
    //access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        
        didSet {
            
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //当前授权用户的UID。
    var uid: String?
    
    //过期时间
    var expiresDate: NSDate?
    
    //用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    //用户昵称
    var screen_name: String?
    
    init(dic: [String : AnyObject]) {
        
        super.init()

        setValuesForKeysWithDictionary(dic)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //MARK: - 归档与解档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        
        aCoder.encodeObject(uid, forKey: "uid")
        
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    override var description: String {
        
        get {
            
            let key = ["access_token", "expires_in", "expiresDate", "uid", "avatar_large", "screen_name"]
            
            return "\n\t模型数据: \(dictionaryWithValuesForKeys(key).description)"
        }
    }
}



/*
access_token	string	用于调用access_token，接口获取授权后的access token。
expires_in	string	access_token的生命周期，单位是秒数。
remind_in	string	access_token的生命周期（该参数即将废弃，开发者请使用expires_in）。
uid	string	当前授权用户的UID。

["access_token": 2.00HbDnlC00X8mudf77dc32469HsLSD,
"remind_in": 157679999,
"uid": 2538445361,
"expires_in": 157679999]
*/